package com.song.controller.admin;

import com.song.dto.CategoryPageQueryDTO;
import com.song.entity.Category;
import com.song.result.PageResult;
import com.song.result.Result;
import com.song.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("admin-CategoryController")
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //修改分类
    @PutMapping
    public Result update(@RequestBody Category  category){
     Integer result =  categoryService.updateCategory(category);
     return result == 1 ? Result.success() : Result.error("修改失败");
    }

    //删除分类
    @DeleteMapping
    public Result deleteCategory(Long id){
      Integer result =  categoryService.deleteCategory(id);
      return result == 1 ? Result.success() : Result.error("删除失败");
    }

    //分类分页查询
    @GetMapping("/page")
    public Result<PageResult<Category>> getCategoryPage( CategoryPageQueryDTO categoryPageQueryDTO){
        return Result.success(categoryService.getCategoryPage(categoryPageQueryDTO),"分类查询成功");
    }

    //修改分类状态
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,@RequestParam Long id){
        Integer result =  categoryService.updateCategoryStatus(status,id);
        return result == 1 ? Result.success(null,"修改分类状态成功") : Result.error("修改失败");
    }

    //新增分类
    @PostMapping
    public Result addCategory(@RequestBody Category category){
        Integer result =  categoryService.addCategory(category);
        return result == 1 ? Result.success(null,"添加分类成功") : Result.error("添加分类失败");
    }

    //根据类型查询分类列表
    @GetMapping("/list")
    public Result<List<Category>> getCategoryByType(Integer type){
        return Result.success(categoryService.getCategoryByType(type),"查询成功");
    }

}
